﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace katOS
{
    class PriorityQueue: AbstractQueue<Process>
    {
        public PriorityQueue(int id, string name, int quantum, bool preemptive, int queueOnEnd, int queueOnIOEnd)
        {
            this.id = id;
            this.name = name;
            this.quantum = quantum;
            this.preemptive = preemptive;
            this.queueOnQuantumEnd = queueOnEnd;
            this.queueOnIOEnd = queueOnIOEnd;
        }

        public override Process getElement(int i)
        //public Process getElement(int i)
        {
            return this.list.ElementAt(i);
        }

        public override Process getLastProcess()
        //public Process getLastProcess()
        {
            return this.lastone_in;
        }

        public override Process getFirst()
        {
            /*for (int i = 0; i < list.Count; i++ )
            {
                Console.WriteLine("i: "+i+" Count: "+list.Count+" FUERA DEL IF ID: " + list.ElementAt(i).getPID() + " Name: " + list.ElementAt(i).getFileName());
            }*/
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    //Console.WriteLine("i: " + i + " Count: " + list.Count + " DENTRO DEL IF ID: " + list.ElementAt(i).getPID() + " Name: " + list.ElementAt(i).getFileName());
                }
                return list.First.Value;
            }
            return null;
        }

        public override void addProcess(Process new_process)
        //public void addProcess(Process new_process)
        {
            //Console.WriteLine("is reaching the method add of a priority queue");
            //new_process.queue = queue_number;
            this.list.AddLast(new_process);
            this.lastone_in = new_process;
            Process temp = list.ElementAt(0);
            LinkedList<Process> temp_list = new LinkedList<Process>();
            SortedList<int, Process> sortedlist = new SortedList<int, Process>();

            for (int i = 0; i< this.list.Count; i++)
            {
                //Console.WriteLine("list count: "+list.Count);
                sortedlist.Add(list.ElementAt(i).priority, list.ElementAt(i));
            }

            list = new LinkedList<Process>();

            for (int i = 0; i < sortedlist.Count; i++)
            {
                list.AddLast(sortedlist.ElementAt(i).Value);
            }

            this.lastone_in = list.ElementAt(list.Count-1);

            if (!preemptive && list.Count != 1)
            {
                this.list.Remove(temp);
                this.list.AddFirst(temp);
            }

            this.lastone_in.setQuantumLeft(quantum);
            this.lastone_in.setState(2);
            //this.lastone_in.setQueue(getQueueNumber());
            this.lastone_in.io_wait = false;

        }

        /*private LinkedList<Process> Sorting(LinkedList<Process> to_sort)
        {
            Process temp1, temp2;
            for (int i = 0; i < to_sort.Count; i++)
            {
                if (to_sort.ElementAt(i).priority > to_sort.ElementAt(i + 1).priority)
                {
                    temp1 = to_sort.ElementAt(i);
                    temp2 = to_sort.ElementAt(i + 1);

                    to_sort.AddBefore(to_sort.Find(temp2), temp2);
                    to_sort.AddAfter(to_sort.Find(temp2), temp1);
                    to_sort.Remove(to_sort.ElementAt(i));
                    to_sort.Remove(to_sort.ElementAt(i + 2));
                }
            }
            return to_sort;

        */
    }
}
